package com.mingrisoft;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class drawRealTimeMapActivity extends Activity {
    private mqttManager mqtt_manager;
    private String subsrcibe_topic = "controlDevice";
    private String publishTopic = "controlDevice";
    private TextView[] tvs = {};
    private Timer timer;
    private LineChart mChart, humLineChart, lightLineChart, co2LineChart;
    private ArrayList<Entry> dataValues = new ArrayList<>();
    private ArrayList<Entry> shumEntries = new ArrayList<>();
    private ArrayList<Entry> lightEntries = new ArrayList<>();
    private ArrayList<Entry> co2Entries = new ArrayList<>();
    private int xAxisCounter = 0,xAxisCounter1=0,xAxisCounter2=0,xAxisCounter3=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_real_time_map);
        mChart = (LineChart) findViewById(R.id.lineChart1);
        humLineChart = (LineChart) findViewById(R.id.lineChart2);
        co2LineChart = (LineChart) findViewById(R.id.lineChart3);
        lightLineChart = (LineChart) findViewById(R.id.lineChart4);

        mqtt_manager = mqttManager.getInstance(tvs, 0);
        mqtt_manager.flashAbtrr(tvs, 6);
        mqtt_manager.subscribeMessage(subsrcibe_topic, 1);
        mqtt_manager.mqqtBack();
        timer = new Timer();
        timerClearData();
        initChart();
        startGeneratingData();

    }

    private void initChart() {
        mChart.setTouchEnabled(true);
        mChart.setPinchZoom(true);
        mChart.setDrawGridBackground(false);

        humLineChart.setTouchEnabled(true);
        humLineChart.setPinchZoom(true);
        humLineChart.setDrawGridBackground(false);

        co2LineChart.setTouchEnabled(true);
        co2LineChart.setPinchZoom(true);
        co2LineChart.setDrawGridBackground(false);

        lightLineChart.setTouchEnabled(true);
        lightLineChart.setPinchZoom(true);
        lightLineChart.setDrawGridBackground(false);

        Description description = new Description();
        Description description1 = new Description();
        Description description2 = new Description();
        Description description3 = new Description();
        description.setText("the Temperature-Real-time of Chart");
        description1.setText("the Humidity-Real-time of Chart");
        description2.setText("the Co2-Real-time of Chart");
        description3.setText("the Light-Real-time of Chart");
        mChart.setDescription(description);
        humLineChart.setDescription(description1);
        co2LineChart.setDescription(description2);
        lightLineChart.setDescription(description3);

        LineData data = new LineData();
        LineData data1 = new LineData();
        LineData data2 = new LineData();
        LineData data3 = new LineData();
        mChart.setData(data);
        humLineChart.setData(data1);
        co2LineChart.setData(data2);
        lightLineChart.setData(data3);

        XAxis xAxis = mChart.getXAxis();
        XAxis xAxis1 = humLineChart.getXAxis();
        XAxis xAxis2 = co2LineChart.getXAxis();
        XAxis xAxis3 = lightLineChart.getXAxis();

        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis2.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis3.setPosition(XAxis.XAxisPosition.BOTTOM);

        YAxis leftAxis = mChart.getAxisLeft();
        YAxis leftAxis1 = humLineChart.getAxisLeft();
        YAxis leftAxis2 = co2LineChart.getAxisLeft();
        YAxis leftAxis3 = lightLineChart.getAxisLeft();

        leftAxis.setAxisMinimum(0f);
        leftAxis1.setAxisMinimum(0f);
        leftAxis2.setAxisMinimum(400f);
        leftAxis3.setAxisMinimum(0f);

        leftAxis.setAxisMaximum(100f);
        leftAxis1.setAxisMaximum(100f);
        leftAxis2.setAxisMaximum(2000f);
        leftAxis3.setAxisMaximum(500f);

        YAxis rightAxis = mChart.getAxisRight();
        YAxis rightAxis1 = humLineChart.getAxisRight();
        YAxis rightAxis2 = co2LineChart.getAxisRight();
        YAxis rightAxis3 = lightLineChart.getAxisRight();

        rightAxis.setEnabled(false);
        rightAxis1.setEnabled(false);
        rightAxis2.setEnabled(false);
        rightAxis3.setEnabled(false);

        mChart.invalidate();
        humLineChart.invalidate();
        co2LineChart.invalidate();
        lightLineChart.invalidate();
    }

    private void startGeneratingData() {
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                generateData();
                handler.postDelayed(this, 2000); // Generate data every 1 second
            }
        }, 1000); // Initial delay of 1 second
    }

    private void generateData() {
        float tempValue = (float) (Math.random() * 100);
        float humValue = (float) (Math.random() * 100);
        float co2Value = (float) (Math.random() * 100);
        float lightValue = (float) (Math.random() * 100);
        tempValue=mqtt_manager.getTempValue();
        humValue=mqtt_manager.getShumValue();
        co2Value=mqtt_manager.getCo2Value();
        lightValue=mqtt_manager.getLightValue();

        dataValues.add(new Entry(xAxisCounter++, tempValue));
        shumEntries.add(new Entry(xAxisCounter1++, humValue));
        co2Entries.add(new Entry(xAxisCounter2++, co2Value));
        lightEntries.add(new Entry(xAxisCounter3++, lightValue));

        LineDataSet set = new LineDataSet(dataValues, "temperature Data");
        LineDataSet set1 = new LineDataSet(shumEntries, "humidity Data");
        LineDataSet set2 = new LineDataSet(co2Entries, "co2 Data");
        LineDataSet set3 = new LineDataSet(lightEntries, "light Data");

        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set3.setMode(LineDataSet.Mode.CUBIC_BEZIER);

        set.setDrawFilled(true);
        set1.setDrawFilled(true);
        set2.setDrawFilled(true);
        set3.setDrawFilled(true);

        set.setFillColor(getResources().getColor(R.color.cadetblue));
        set1.setFillColor(getResources().getColor(R.color.red));
        set2.setFillColor(getResources().getColor(R.color.greenyellow));
        set3.setFillColor(getResources().getColor(R.color.cadetblue));

        set.setColor(getResources().getColor(R.color.yellow));
        set1.setColor(getResources().getColor(R.color.blue));
        set2.setColor(getResources().getColor(R.color.black));
        set3.setColor(getResources().getColor(R.color.orange));

        set.setLineWidth(1f);
        set1.setLineWidth(1f);
        set2.setLineWidth(1f);
        set3.setLineWidth(1f);

        set.setDrawValues(false);
        set1.setDrawValues(false);
        set2.setDrawValues(false);
        set3.setDrawValues(false);

        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        ArrayList<ILineDataSet> dataSets1 = new ArrayList<>();
        ArrayList<ILineDataSet> dataSets2 = new ArrayList<>();
        ArrayList<ILineDataSet> dataSets3 = new ArrayList<>();

        dataSets.add(set);
        dataSets1.add(set1);
        dataSets2.add(set2);
        dataSets3.add(set3);
        LineData data = new LineData(dataSets);
        LineData data1 = new LineData(dataSets1);
        LineData data2 = new LineData(dataSets2);
        LineData data3 = new LineData(dataSets3);
        mChart.setData(data);
        humLineChart.setData(data1);
        co2LineChart.setData(data2);
        lightLineChart.setData(data3);

        mChart.notifyDataSetChanged();
        humLineChart.notifyDataSetChanged();
        co2LineChart.notifyDataSetChanged();
        lightLineChart.notifyDataSetChanged();

        mChart.invalidate();
        humLineChart.invalidate();
        co2LineChart.invalidate();
        lightLineChart.invalidate();
    }

    private void timerClearData() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 清理数据
                dataValues.clear();
                shumEntries.clear();
                lightEntries.clear();
                co2Entries.clear();
            }
        }, 0, 100000); // 60秒钟执行一次清理操作
    }
}

